using System;
using System.Collections.Generic;
using BookingSystem.Entities;
using BookingSystem.Repository;
using NHibernate.Criterion;

namespace BookingSystem.Services
{
    public class ResourceService
    {
        private readonly RepositoryBase<Resource> _repository;

        public ResourceService()
            : this(new RepositoryBase<Resource>())
        {
        }

        public ResourceService(RepositoryBase<Resource> repository)
        {
            _repository = repository;
        }

        public void Save(Resource resource)
        {
            _repository.Session.SaveOrUpdate(resource);
            _repository.Session.Flush();
        }

        public Resource Get(int id)
        {
            return _repository.GetById(id);
        }

        public IList<Resource> GetAll()
        {
            return _repository.GetAll();
        }

        public Resource GetByName(string name)
        {
            return _repository.Session.CreateCriteria((typeof(Resource)))
             .Add(Restrictions.InsensitiveLike("Name", name))
             .AddOrder(Order.Asc("Name")).UniqueResult<Resource>();
        }
    }
}